package org.example.volunteerend.controller;

import lombok.RequiredArgsConstructor;
import org.example.volunteerend.common.Result;
import org.example.volunteerend.dto.PageResponse;
import org.example.volunteerend.entity.PointsRecord;
import org.example.volunteerend.entity.ServiceRecord;
import org.example.volunteerend.service.UserRecordService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/users")
@RequiredArgsConstructor
public class UserRecordController {
    private final UserRecordService userRecordService;

    @GetMapping("/{userId}/service-records")
    public Result<Map<String, Object>> getServiceRecords(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResponse<ServiceRecord> pageResponse = userRecordService.getServiceRecords(userId, pageNum, pageSize);
        
        // 转换为符合规范的响应格式
        List<Map<String, Object>> rows = pageResponse.getRecords().stream().map(record -> {
            Map<String, Object> row = new HashMap<>();
            row.put("record_id", record.getRecordId());
            row.put("project_id", record.getProjectId());
            row.put("project_name", record.getProjectName());
            row.put("service_date", record.getServiceDate() != null ? record.getServiceDate().toString() : null);
            row.put("service_hours", record.getServiceHours());
            row.put("description", record.getDescription());
            row.put("proof_images", record.getProofImages());
            return row;
        }).collect(Collectors.toList());
        
        Map<String, Object> data = new HashMap<>();
        data.put("total", pageResponse.getTotal());
        data.put("rows", rows);
        
        return Result.success(data);
    }

    @GetMapping("/{userId}/points-records")
    public Result<Map<String, Object>> getPointsRecords(
            @PathVariable Integer userId,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResponse<PointsRecord> pageResponse = userRecordService.getPointsRecords(userId, pageNum, pageSize);
        
        // 转换为符合规范的响应格式
        List<Map<String, Object>> rows = pageResponse.getRecords().stream().map(record -> {
            Map<String, Object> row = new HashMap<>();
            row.put("record_id", record.getRecordId());
            row.put("points", record.getPoints());
            row.put("reason", record.getReason());
            row.put("create_time", record.getCreateTime() != null ? record.getCreateTime().toString() : null);
            return row;
        }).collect(Collectors.toList());
        
        Map<String, Object> data = new HashMap<>();
        data.put("total", pageResponse.getTotal());
        data.put("rows", rows);
        
        return Result.success(data);
    }
} 